﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
    public class User
    {
        public string UserName { get; }
        public string Password { get; set; }
        public int Role { get; set; }

        public readonly List<BorrowRecord> BorrowRecords;

        public User(string username, string password, int role)
        {
            UserName = username;
            Password = password;
            Role = role;
            BorrowRecords = new();
        }

        public void BorrowBook(string title, int quantity)
        {
            var existingRecord = BorrowRecords.Find(record => record.Title == title);
            if (existingRecord != null) existingRecord.Quantity += quantity;
            else BorrowRecords.Add(new(title, quantity));
        }

        public bool ReturnBook(string title, int quantity)
        {
            var existingRecord = BorrowRecords.Find(record => record.Title == title);
            if (existingRecord != null)
            {
                if (existingRecord.Quantity > quantity)
                {
                    Console.WriteLine("超出借阅数量!");
                    return false;
                }
                if (existingRecord.Quantity - quantity == 0) BorrowRecords.Remove(existingRecord);
                else existingRecord.Quantity -= quantity;
                return true;
            }
            Console.WriteLine($"未借阅书籍 《{title}》");
            return false;
        }

        // 分页 待处理
        public void DisplayBorrowedBooks()
        {
            Console.WriteLine($"用户: {UserName}");
            if (BorrowRecords.Count == 0) Console.WriteLine("暂无借阅记录!");
            else
            {
                foreach (var bookRecord in BorrowRecords)
                {
                    Console.WriteLine(bookRecord);
                }
            }
        }
    }
}
